package cn.neusoft.service.impl;

import cn.neusoft.mapper.BusinessMapper;
import cn.neusoft.pojo.Business;
import cn.neusoft.query.BaseQuery;
import cn.neusoft.query.BusinessQuery;
import cn.neusoft.service.IBusinessService;
import cn.neusoft.vo.PageBean;
import cn.neusoft.vo.ResultBean;
import com.alibaba.druid.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class BusinessService implements IBusinessService {

    @Autowired
    BusinessMapper businessMapper;

    @Transactional
    @Override
    public ResultBean add(Business business) {

        if (business==null ||
                StringUtils.isEmpty(business.getBusinessAccount()) ||
                StringUtils.isEmpty(business.getBusinessName())){
            return ResultBean.ERROR("请求参数为空值");
        }

        int add = businessMapper.add(business);

        return add>0?ResultBean.SUCCESS(business.getBusinessId()):ResultBean.ERROR("添加商家信息失败");
    }

    @Transactional
    @Override
    public ResultBean update(Business business) {

        if (business==null ||
                business.getBusinessId()==null ||
                business.getBusinessId()==0 ||
                StringUtils.isEmpty(business.getBusinessAccount()) ||
                StringUtils.isEmpty(business.getBusinessName())){
            return ResultBean.ERROR("请求参数为空值");
        }

        int update = businessMapper.update(business);

        return update>0?ResultBean.SUCCESS():ResultBean.ERROR("修改商家信息失败");
    }

    @Transactional
    @Override
    public ResultBean delete(Integer id) {

        if (id==null){
            return ResultBean.ERROR("请求参数为空值");
        }

        int delete = businessMapper.delete(id);

        return delete>0?ResultBean.SUCCESS():ResultBean.ERROR("删除商家信息失败");
    }

    @Transactional(readOnly = true)
    @Override
    public ResultBean getById(Integer id) {

        if (id==null){
            return ResultBean.ERROR("请求参数为空值");
        }

        Business business = businessMapper.getById(id);

        return business!=null?ResultBean.SUCCESS(business):ResultBean.ERROR("查询商家信息失败");
    }

    @Transactional(readOnly = true)
    @Override
    public ResultBean getAll() {
        List<Business> businessList = businessMapper.getAll();
        return ResultBean.SUCCESS(businessList);
    }

    @Transactional(readOnly = true)
    @Override
    public ResultBean getByPage(BaseQuery baseQuery) {

        if (baseQuery == null){
            baseQuery = new BusinessQuery();
        }

        PageBean<Business> pageBean = new PageBean<>();
        int count = businessMapper.getCount(baseQuery);
        pageBean.setTotalRows(count);

        List<Business> businessList = businessMapper.getByPage(baseQuery);
        pageBean.setData(businessList);


        return ResultBean.SUCCESS(pageBean);
    }

    @Transactional
    @Override
    public ResultBean batchDel(List<Integer> ids) {

        if (ids==null || ids.size()==0){
            return ResultBean.ERROR("请求参数为空值");
        }

        for (Integer id:ids){
            int delete = businessMapper.delete(id);
            if (delete==0){
                throw new RuntimeException("删除失败");
            }
        }

        return ResultBean.SUCCESS();
    }
}
